<?php
namespace Home\Controller;
use Home\Service\WechatService;
use Common\Controller\HomebaseController;
// +----------------------------------------------------------------------
// | Date:2017年2月17日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class PublicController extends HomebaseController{
    
    public function login(){
        $u_account = I('u_account');
        $u_password = I('u_password');
        
        $accountService = D('Account','Service');
        $ret = $accountService->login($u_account,$u_password,'sn,position_lvl_id,leader_id,dep_id,dep_name,ep_id');
        if (!$ret) $this->error($accountService->getError());

        $this->success('登录成功！');
        
       
    }
    
    
    
    public function logout(){
        session('user',null);
        cookie('user',null);
        $this->success('退出成功！');
    }
    
    /**
     * 获取订单详情
     * id :订单编号
     * code:订单类型编码
     *
     * date:2017年6月12日
     * author: EK_熊
     */
    public function detail(){
        $orderSn = I('sn');
        
       // $this->_set_model($code);//获取模型
        $map['order_sn'] = $orderSn;
        $info = M('Order')->where($map)->find();
        if (!$info) $this->error('获取不到数据，可能参数错误，或者数据不存在');
        if (isset($info['file']) && $info['file']){
            $info['file'] = json_decode($info['file'],true);
            if ($info['file']) {
                foreach ($info['file'] as $k=>$v){
                        unset($info['file'][$k]);
                    for($i=0;$i<count($v);$i++) {
                        $info['file'][$k][$i]['path'] = $v[$i];
                        $info['file'][$k][$i]['download'] = U('download',['path'=>$v[$i],'.']);
                    }
                }
            }
        }
        if ($info['expand']) {
            $expand = json_decode($info['expand'],true);
            foreach ($expand as $k=>$v){
                $info['expand_'.$k] = $v;
            }
        }
        $data['detail'] = $info;
        $this->ajaxReturn($data);
    
    }
    
    /**
     * 下载文件
     * 
     * date:2017年6月15日
     * author: EK_熊
     */
    public function download(){
        $path = ltrim(I('path'),'.');
        $filename = basename($path);
        $path = realpath("../$path");
        $file=fopen("$path","r");
        header("Content-Type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".filesize($path));
        header("Content-Disposition: attachment; filename=$filename");
        echo fread($file,filesize($path));
        fclose($file);

        
    }
    
}